package com.suixun;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.StringUtil;
import com.suixun.queue.BaseDeal;
import com.suixun.util.JWTHelper;


/**
 * Constants.java
 * 
 * 定义的一些会用的常量
 * 
 * @author <a href="mailto:huangy@17find.com">hellmash</a>
 * @version create on 2015-07-20 10:31
 *          <p>
 *          CopyRight(c)2015 杭州随寻科技有限公司 17find.com 版权所有
 */
public class Constants {

    // http 相关
    public final static String CONTENT_TYPE_JSON = "application/json";
    public final static String CACHE_CONTROL = "Cache-Control";
    public final static String NO_CACHE = "no-store";
    public final static String ACCESS_TOKEN = "access_token";
    public final static String TOKEN_TYPE = "token_type";
    public final static String TOKEN_SCOPE = "token_scope";
    public final static String EXPIRES_IN = "expires_in";
    public final static String GET = "GET";
    public final static String POST = "POST";
    public final static String PUT = "PUT";
    public final static String DELETE = "DELETE";
    public final static String AUTHORIZATION = "Authorization";
    public static String IMAGE_URL = "";
    private static String UPLOADPIC_URL = ""; 

    // media
    public static String MEDIA_HTTP_URL = "http://api.17find.com/media";
    //public static String MEDIA_HTTP_URL = "http://192.168.2.13/media";
    public static Integer MEDIA_MAX_SIZE = 1024 * 1024 * 5; // 5M
    public static String[] MEDIA_FILE_EXTS = {"gif", "png", "jpeg", "jpg", "mp3", "amr", "wav",
            "awb"};

    private static String PLATFORM_ENV = "DEV";
    public static Map<String, List<BigDecimal>> RENEWAL_DEVICE_PRICES = new HashMap<String, List<BigDecimal>>();;
    public static BigDecimal[] RENEWAL_PACKAGE_FEES = {};
    public static HashMap<String, BigDecimal> PURCHASE_DEVICE_PRICES = null;
    public static HashMap<String, String> THIRD_PARTY_RESOURCES = null;
    static {
        Properties prop = new Properties();
        String path = BaseDeal.class.getClassLoader().getResource("webconfig.properties").getPath();
        try {
            InputStream is = new FileInputStream(path);
            prop.load(is);
            if (prop.getProperty("PLATFORM_ENV") != null)
                PLATFORM_ENV = prop.getProperty("PLATFORM_ENV").trim();
            
            ObjectMapper mapper = new ObjectMapper();
            mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
            if (prop.getProperty("RENEWAL_DEVICE_PRICES") != null)
                RENEWAL_DEVICE_PRICES = mapper.readValue(prop.getProperty("RENEWAL_DEVICE_PRICES").trim(),new HashMap<String, List<BigDecimal>>().getClass());
            
            if (prop.getProperty("RENEWAL_PACKAGE_FEES") != null)
                RENEWAL_PACKAGE_FEES = mapper.readValue(prop.getProperty("RENEWAL_PACKAGE_FEES").trim(),BigDecimal[].class);
            
            if (prop.getProperty("PURCHASE_DEVICE_PRICES") != null)
                PURCHASE_DEVICE_PRICES = mapper.readValue(prop.getProperty("PURCHASE_DEVICE_PRICES").trim(),HashMap.class);
            
            if (prop.getProperty("THIRD_PARTY_RESOURCES") != null)
                THIRD_PARTY_RESOURCES = mapper.readValue(prop.getProperty("THIRD_PARTY_RESOURCES").trim(),HashMap.class);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    public static String get_platform_env() {
        return PLATFORM_ENV;
    }

    public static String getMediaSavePath() throws IOException {
        String mediapath = "";
        Properties prop = new Properties();
        String path = BaseDeal.class.getClassLoader().getResource("webconfig.properties").getPath();
        InputStream is = new FileInputStream(path);
        prop.load(is);
        mediapath = prop.getProperty("media.savepath").trim();
        return mediapath;
    }
    
    public static String getApiUrl() throws IOException{
    	
    	
    	String apiurl = "";
        Properties prop = new Properties();
        String path = BaseDeal.class.getClassLoader().getResource("webconfig.properties").getPath();
        InputStream is = new FileInputStream(path);
        prop.load(is);
        apiurl = prop.getProperty("API_URL").trim();
        return apiurl;
    }
    
    public static String getImageUrl(){
    	try{
    		if(StringUtil.isNotEmpty(IMAGE_URL)){
    			return IMAGE_URL;
    		}
            Properties prop = new Properties();
            String path = BaseDeal.class.getClassLoader().getResource("webconfig.properties").getPath();
            InputStream is = new FileInputStream(path);
            prop.load(is);
            IMAGE_URL = prop.getProperty("IMAGE_URL").trim();
            return IMAGE_URL;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public static String getUploadPicUrl(){
    	try{
    		if(StringUtil.isNotEmpty(UPLOADPIC_URL)){
    			return UPLOADPIC_URL;
    		}
            Properties prop = new Properties();
            String path = BaseDeal.class.getClassLoader().getResource("webconfig.properties").getPath();
            InputStream is = new FileInputStream(path);
            prop.load(is);
            UPLOADPIC_URL = prop.getProperty("UEDITOR_UPLOADPIC_URL").trim();
            return UPLOADPIC_URL;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public static String getToken() {

		JWTHelper jWTHelper = new JWTHelper();
		return jWTHelper.createJWT("8756", "13868113229", "123456", 0l, "guardian");
	}
}
